﻿app.controller('ConfigCtrl', [
    '$scope', 'alertService', 'configService',
    function($scope,alertService,configService) {
        configService.getAllAsync()
            .then(function(result) {
                $scope.configList = result;
            }, alertService.error);

        $scope.update = function (config) {
            configService.updateAsync(config.Key, config.Content)
                 .then(function (result) {
                    alertService.add('更新成功', '提示', 'success');
                }, alertService.error);
        };

        $scope.editotrConfig = {
            //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个
            toolbars: [['FullScreen', 'Source', 'Undo', 'Redo', 'Bold', 'test']],
            //focus时自动清空初始化时的内容
            autoClearinitialContent: true,
            //关闭字数统计
            wordCount: true,
            //关闭elementPath
            elementPathEnabled: false
        }
    }
]);

app.factory('configService', [
    '$http', '$q',
    function($http, $q) {
        var self = {};

        self.getAllAsync = function() {
            var defer = $q.defer();

            $http({
                url: '/api/Config'
            }).success(function(result) {
                if (result.Code >= 0) defer.resolve(result.Data);
                else defer.reject(result.Message);
            }).error(function(x) {
                defer.reject('查询参数列表失败，请稍后再试');
            });

            return defer.promise;
        };

        self.updateAsync = function (key,content) {
            var defer = $q.defer();

            $http({
                method:'POST',
                url: '/api/Config',
                data:{Key:key,Content:content}
            }).success(function (result) {
                if (result.Code >= 0) defer.resolve(result.Data);
                else defer.reject(result.Message);
            }).error(function (x) {
                defer.reject('更新参数失败，请稍后再试');
            });

            return defer.promise;
        };

        return self;
    }
]);